package com.cty.user.controller;

import com.cty.param.UserCheckParam;
import com.cty.param.UserLoginParam;
import com.cty.pojo.User;
import com.cty.user.service.UserService;
import com.cty.utils.R;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//用户模块的控制器
//返回json数据
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    //检查账号是否可用接口
    //BindingResult result 获得校验结果的对象
    @PostMapping(value = "/check")
    public R check(@RequestBody @Validated UserCheckParam userCheckParam, BindingResult result){
        if(result.hasErrors()){
           return R.fail("账号为null,不可使用！");
        }
        return userService.check(userCheckParam);

    }

    //账号注册接口
    @PostMapping(value = "/register")
    public R register(@RequestBody @Validated User user,BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常，不可注册");
        }
        return userService.register(user);
    }

    //用户登录接口
    @PostMapping(value = "/login")
    public R login(@RequestBody @Validated UserLoginParam userLoginParam,BindingResult result){
        if(result.hasErrors()){
            return R.fail("参数异常，不可登录");
        }
        return userService.login(userLoginParam);
    }
}
